//
//  MainViewController.swift
//  swift_wobo
//
//  Created by yuxin on 2018/11/13.
//  Copyright © 2018 wuxiantianli. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        addChildViewControllers()
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        setupComposeBtn();
    }
    
    @objc private func composeBtnClick(btn:UIButton){
        let vc = YXPlayerViewController()
        let navi = UINavigationController(rootViewController: vc)
        navi.navigationBar.barTintColor = UIColor.black
        present(navi, animated: true, completion: nil)
    }
    
    
    func addChildViewControllers() {
        
        let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil)
        let jsonData = NSData(contentsOfFile: jsonPath!)! as Data
       
        
        do{
             let dictArr = try JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions.mutableContainers) as! [[String:String]]
            
            for dict in dictArr {
                let vcName: String? = dict["vcName"]
                let title = dict["title"]!
                let imageName = dict["imageName"]!
                
                addChildViewController(childControllerName: vcName!, title: title, imageName: imageName)
            }
        }catch{
            addChildViewController(childControllerName: "HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController(childControllerName: "MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController(childControllerName: "DiscoverTableViewController", title: "广场", imageName: "tabbar_discover")
            addChildViewController(childControllerName: "ProfileTableViewController", title: "我", imageName: "tabbar_profile")
        }
        
    }
    
    private func addChildViewController(childControllerName: String, title:String, imageName:String) {
        
        let nameSpace = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
       
        let cls:AnyClass = NSClassFromString(nameSpace + "." + childControllerName)!
        
        let vcCls = cls as! UIViewController.Type
        
        let vc = vcCls.init()
        
        vc.title = title;
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        
        let navi = UINavigationController()
        navi.addChild(vc)
        addChild(navi)

    }

    private func setupComposeBtn() {
        
        let with = tabBar.bounds.width / CGFloat(viewControllers!.count)

        let rect = CGRect(x: with * 2, y: 0, width: with, height: tabBar.bounds.height)
        composeBtn.frame = rect;
        
        tabBar.addSubview(composeBtn)
    }
    
    private lazy var composeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "tabbar_compose_icon_add"), for: UIControl.State.normal)
        btn.setImage(UIImage(named: "tabbar_compose_icon_add_highlighted"), for: UIControl.State.highlighted)
        
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button"), for: UIControl.State.normal)
        btn.setBackgroundImage(UIImage(named: "tabbar_compose_button_highlighted"), for: UIControl.State.highlighted)
        
        btn.addTarget(self, action: #selector(composeBtnClick(btn:)), for: .touchUpInside)
        return btn
    }()


}
